package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.*;
import com.jdrx.sw.sewage.beans.dto.base.PageDTO;
import com.jdrx.sw.sewage.beans.vo.FactoryPeriodsVO;
import com.jdrx.sw.sewage.beans.vo.MaintenanceDevVO;
import com.jdrx.sw.sewage.beans.vo.MaintenanceRecordInfoVO;
import com.jdrx.sw.sewage.beans.vo.MaintenanceRecordVO;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.MaintenanceServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/maintenance")
@Api(value = "维修保养工单模块")
public class MaintenanceApi {

    @Autowired
    private MaintenanceServiceImpl maintenanceServiceImpl;

    @ApiOperation(value = "最近待保养设备列表")
    @RequestMapping(value = "findMaintenanceDevList", method = RequestMethod.POST)
    public ResponseVO<PageVO<MaintenanceDevVO>> findMaintenanceDevList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                       @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                       @RequestBody @Valid PageDTO pageDTO) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findMaintenanceDevList(pageDTO));
    }

    @ApiOperation(value = "我得待办/全部待办 我的待办userId传当前人id，全部不筛选人传null/筛选传筛选人id")
    @RequestMapping(value = "findNoDo", method = RequestMethod.GET)
    public ResponseVO<Map<String, Object>> findNoDo(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                    @RequestParam Long userId) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findNoDo(userId));
    }

    @ApiOperation(value = "我得待办/全部待办数量查询 我的待办userId传当前人id，全部不筛选人传null/筛选传筛选人id")
    @RequestMapping(value = "findNoDoNum", method = RequestMethod.GET)
    public ResponseVO<Integer> findNoDoNum(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                           @RequestHeader(Constant.TENANT_ID) String tenantId,
                                           @RequestParam Long userId) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findNoDoNum(userId));
    }

    @ApiOperation(value = "查询厂区和工艺单元和品类")
    @RequestMapping(value = "findFactoryPeriods", method = RequestMethod.GET)
    public ResponseVO<List<FactoryPeriodsVO>> findFactoryPeriods(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                 @RequestHeader(Constant.TENANT_ID) String tenantId) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findFactoryPeriods());
    }

    @ApiOperation(value = "根据厂区+工艺+品类查询设备设备")
    @RequestMapping(value = "finMaintenanceDevByFp", method = RequestMethod.GET)
    public ResponseVO<List<MaintenanceDevVO>> finMaintenanceDevByFp(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                    @RequestParam("factoryPeriods") String factoryPeriods,
                                                                    @RequestParam("processunitId") Long processunitId,
                                                                    @RequestParam("categoryId") Long categoryId,
                                                                    @RequestParam("type") Integer type) {
        return ResponseFactory2.ok(maintenanceServiceImpl.finMaintenanceDevByFp(factoryPeriods, processunitId, categoryId, type));
    }

    @ApiOperation(value = "派单")
    @RequestMapping(value = "dispatch", method = RequestMethod.POST)
    public ResponseVO dispatch(@RequestHeader(Constant.DATA_NODE) String dataNode,
                               @RequestHeader(Constant.TENANT_ID) String tenantId,
                               @RequestBody @Valid DispatchDTO dispatchDTO) {
        maintenanceServiceImpl.dispatch(dispatchDTO);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "根据用户id查询工单时间安排表")
    @RequestMapping(value = "findUserMaintenanceTime", method = RequestMethod.GET)
    public ResponseVO<List> findUserMaintenanceTime(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                    @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                    @RequestParam("maintenanceById") Long maintenanceById) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findUserMaintenanceTime(maintenanceById));
    }

    @ApiOperation(value = "根据用户id和时间查询已有工单")
    @RequestMapping(value = "findUserMaintenance", method = RequestMethod.POST)
    public ResponseVO<List<MaintenanceDevVO>> findUserMaintenance(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                  @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                  @RequestBody @Valid UserMaintenanceDTO userMaintenanceDTO) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findUserMaintenance(userMaintenanceDTO));
    }

    @ApiOperation(value = "保养/维修申请")
    @RequestMapping(value = "maintenanceApply", method = RequestMethod.POST)
    public ResponseVO maintenanceApply(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                       @RequestHeader(Constant.TENANT_ID) String tenantId,
                                       @RequestBody @Valid MaintenanceApplyDTO maintenanceApplyDTO) throws BizException {
        return maintenanceServiceImpl.maintenanceApply(maintenanceApplyDTO);
    }

    @ApiOperation(value = "保养/维修")
    @RequestMapping(value = "maintenance", method = RequestMethod.POST)
    public ResponseVO maintenance(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                  @RequestHeader(Constant.TENANT_ID) String tenantId,
                                  @RequestBody @Valid MaintenanceUpdateDTO maintenanceUpdateDTO) {
        maintenanceServiceImpl.maintenance(maintenanceUpdateDTO);
        return ResponseFactory2.ok();
    }

    @ApiOperation(value = "保养/维修记录列表")
    @RequestMapping(value = "findMaintenanceRecordList", method = RequestMethod.POST)
    public ResponseVO<PageVO<MaintenanceRecordVO>> findMaintenanceRecordList(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                             @RequestBody @Valid MaintenanceRecordDTO dto) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findMaintenanceRecordList(dto));
    }

    @ApiOperation(value = "保养/维修记录导出")
    @RequestMapping(value = "exportMaintenanceRecord", method = RequestMethod.POST)
    public ResponseVO exportMaintenanceRecord(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                              @RequestHeader(Constant.TENANT_ID) String tenantId,
                                              @RequestBody @Valid MaintenanceRecordDTO2 dto,
                                              HttpServletResponse response) throws BizException {
        maintenanceServiceImpl.exportMaintenanceRecord(response, dto);
        return ResponseFactory2.ok();
    }


    @ApiOperation(value = "查询保养/维修记录详情")
    @RequestMapping(value = "findMaintenanceRecordInfo", method = RequestMethod.GET)
    public ResponseVO<MaintenanceRecordInfoVO> findMaintenanceRecordInfo(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                                                         @RequestHeader(Constant.TENANT_ID) String tenantId,
                                                                         @RequestParam("id") Long id) {
        return ResponseFactory2.ok(maintenanceServiceImpl.findMaintenanceRecordInfo(id));
    }
}
